﻿using CMS.Enbities;
using CMS.Models;
using CMS.View;
using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class CargoViewModel:ViewModelBase
    {
        
        private List<Cardgo> cargos = new List<Cardgo>();
        /// <summary>
        /// 所有物资
        /// 用来在物资管理显示的
        /// </summary>
        public List<Cardgo> Cargos
        {
            get
            {
                return cargos;
            }
            set
            {
                cargos = value;
                RaisePropertyChanged(nameof(Cargos));
            }
        }
        /// <summary>
        /// 物资类型列表
        /// </summary>
        private List<CargoType> cargoTypes = new List<CargoType>();
        public List<CargoType> CargoTypes
        {
            get { return cargoTypes; }
            set { cargoTypes = value; RaisePropertyChanged(nameof(CargoTypes)); }
        }
        /// <summary>
        /// 单位类型列表
        /// </summary>
        private List<UnitType> unitTypes = new List<UnitType>();
        public List<UnitType> UnitTypes
        {
            get { return unitTypes; }
            set { unitTypes = value; RaisePropertyChanged(nameof(UnitTypes)); }
        }
        //初始化刷新数据
        public CargoViewModel()
        {

        }
        /// <summary>
        /// 打开添加新纪录对话框
        /// </summary>
        /// 
        public RelayCommand OpenAddCargoWindowCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    var window = new AddCargoWindow();
                    window.ShowDialog();
                    //刷新数据
                    Cargos = new CargoProvider().Select();//刷新数据
                });
            }
        }
        //删除数据
        public RelayCommand<object> DeleteCargoCommand
        {
            get
            {
                return new RelayCommand<object>((arg) =>
                {
                    if (!(arg is Cardgo model)) return;
                    var count = new CargoProvider().Delete(model);
                    if (count > 0) MessageBox.Show("操作成功", "温馨提示");
                    
                    Cargos = new CargoProvider().Select();//刷新数据
                });
            }
        }
        //编辑数据
        public RelayCommand<object> EditCargoCommand
        {
            get
            {
                return new RelayCommand<object>((arg) =>
                {
                    if (!(arg is Cardgo model)) return;
                    model.Typeld=this.CargoTypes.FirstOrDefault(t=>t.Name==model.TypeName).Id;
                    model.UnitId=this.UnitTypes.FirstOrDefault(t=>t.Name==model.UnitName).Id;
                    var count = new CargoProvider().Update(model);
                    if (count > 0) MessageBox.Show("操作成功", "温馨提示");

                    Cargos = new CargoProvider().Select();//刷新数据
                });
            }
        }
        /// <summary>
        /// 打开入库对话框
        /// </summary>
        public RelayCommand<Cardgo> OpenInputCargoWindowCommand
        {
            get
            {
                return new RelayCommand<Cardgo>((cargo) =>
                {
                  
                    if (cargo == null) return;
                    var inputCargoViewModel=ServiceLocator.Current.GetInstance<InputCargoViewModel>();
                    if (inputCargoViewModel == null) return;
                    inputCargoViewModel.Cargo = cargo;
                    var window = new InputCargoWindow();
                    window.ShowDialog();
                    //刷新数据
                    Cargos = new CargoProvider().Select();
                    var records = new RecordProvider().Select();
                    //遍历这个类
                    Cargos.ForEach(cargoItem =>
                    {
                        var list = records.FindAll(item => item.CargoId == cargoItem.Id);
                        if (list != null)
                        {
                            //统计
                            var inputCount = list.FindAll(item => item.RecoredType == true).Sum(item => item.Number);
                            var outputCount = list.FindAll(item => item.RecoredType == false).Sum(item => item.Number);
                            var sumCount = inputCount - outputCount;
                            cargoItem.Sum = sumCount;
                        }

                    });
                });
            }
        }
        /// <summary>
        /// 打开出库对话框
        /// </summary>
        public RelayCommand<Cardgo> OpenOutputCargoWindowCommand
        {
            get
            {
                return new RelayCommand<Cardgo>((cargo) =>
                {
                    if (cargo == null) return;
                    var outputCargoViewModel = ServiceLocator.Current.GetInstance<OutputCargoViewModel>();
                    if (outputCargoViewModel == null) return;
                    outputCargoViewModel.Cargo = cargo;
                    var window = new OutputCargoWindow();
                    window.ShowDialog();
                    //刷新数据
                    Cargos = new CargoProvider().Select();
                    var records = new RecordProvider().Select();
                    //遍历这个类
                    Cargos.ForEach(cargoItem =>
                    {
                        var list = records.FindAll(item => item.CargoId == cargoItem.Id);
                        if (list != null)
                        {
                            //统计
                            var inputCount = list.FindAll(item => item.RecoredType == true).Sum(item => item.Number);
                            var outputCount = list.FindAll(item => item.RecoredType == false).Sum(item => item.Number);
                            var sumCount = inputCount - outputCount;
                            cargoItem.Sum = sumCount;
                        }

                    });
                });
            }
        }

    }
}
